
//
//  RefreshManager.m
//  lottery
//
//  Created by hzy on 2017/7/22.
//  Copyright © 2017年 hzy. All rights reserved.
//

#import "RefreshManager.h"
@implementation RefreshManager
-(instancetype)initWithurl:(NSString *)urlstring Params:(NSDictionary *)Params{

    self = [super init];
    if (self) {
        
        self.formParams = [NSMutableDictionary dictionaryWithDictionary:Params];
        self.urlstring = urlstring;
        _index = 1;
        
    }
    [self fresh];

    return self;

}

-(void)fresh{

    MJWeakSelf
    
    
    if (!self.headerView) {
        self.headerView = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.index = 1;
            [self refreshParams];
            if (self.beginCompleteBlock) {
                self.beginCompleteBlock();
            }
            if (_isPost == YES) {
                [[TMNetwork sharedInstance] POSTWithJSONRequestSerialized:_urlstring headerParams:nil queryParams:nil formParams:self.formParams withToken:NO model:nil progress:^(NSProgress *progress) {
                    
                } success:^(id responseObject) {
                    [self.headerView endRefreshing];
                    if (responseObject[@"content"]) {
                        
                        if (self.headerCompleteBlock) {
                            self.headerCompleteBlock(responseObject[@"content"]);
                        }
                        
                        
                    }
                    
                } failure:^(id responseObject) {
                    [self.headerView endRefreshing];
                    if (self.failedSuccessBlock) {
                        self.failedSuccessBlock();
                    }
                    
                }];
                
            }else{
            
                [[TMNetwork sharedInstance] GET:_urlstring headerParams:nil queryParams:nil formParams:self.formParams withToken:NO model:nil progress:^(NSProgress *progress) {
                    
                } success:^(id responseObject) {
                    [self.headerView endRefreshing];
                    if (responseObject[@"content"]) {
                        
                        if (self.headerCompleteBlock) {
                            self.headerCompleteBlock(responseObject[@"content"]);
                        }
                        
                        
                    }
                    
                } failure:^(id responseObject) {
                    [self.headerView endRefreshing];
                    if (self.failedSuccessBlock) {
                        self.failedSuccessBlock();
                    }
                    
                }];
            
            }
            
            
            
        }];
    }
    
    
    if (!self.footerView) {
        self.footerView = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            self.index ++;
            [self refreshParams];
            
            if (self.beginCompleteBlock) {
                self.beginCompleteBlock();
            }
            if (_isPost == YES) {
                
                [[TMNetwork sharedInstance] POSTWithJSONRequestSerialized:_urlstring headerParams:nil queryParams:nil formParams:self.formParams withToken:NO model:nil progress:^(NSProgress *progress) {
                    
                } success:^(id responseObject) {
                    [self.footerView endRefreshing];
                    if (responseObject[@"content"]) {
                        
                        if (self.footerCompleteBlock) {
                            self.footerCompleteBlock(responseObject[@"content"]);
                        }
                        
                        
                    }
                    
                } failure:^(id responseObject) {
                    [self.footerView endRefreshing];
                    if (self.failedSuccessBlock) {
                        self.failedSuccessBlock();
                    }
                }];
            }else{
            
                [[TMNetwork sharedInstance] GET:_urlstring headerParams:nil queryParams:nil formParams:self.formParams withToken:NO model:nil progress:^(NSProgress *progress) {
                    
                } success:^(id responseObject) {
                    [self.footerView endRefreshing];
                    if (responseObject[@"content"]) {
                        
                        if (self.footerCompleteBlock) {
                            self.footerCompleteBlock(responseObject[@"content"]);
                        }
                        
                        
                    }
                    
                } failure:^(id responseObject) {
                    [self.footerView endRefreshing];
                    if (self.failedSuccessBlock) {
                        self.failedSuccessBlock();
                    }
                }];
            }
            
            
        }];
    }
    
    
    
    


}
-(void)refreshParams{
    

    [self.formParams setObject:@(_index) forKey:@"pageIndex"];
    [self.formParams setObject:@"20" forKey:@"pageSize"];

    
}

-(void)setRefreshHeader:(UITableView *)scrollView{
    
    scrollView.header = _headerView;
    
    
}
-(void)setRefreshfooter:(UITableView *)scrollView{
    
    scrollView.footer = _footerView;
    
}
@end
